package com.jiwei.system.controller;

import com.jiwei.system.dao.MemberMapper;
import com.jiwei.system.pojo.Member;
import com.jiwei.system.serviceImpl.MemberServiceImpl;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping(value = {"operator", "admin"}, method = RequestMethod.POST)
@Validated
@RestController
public class AdminActionController {

    @Resource(name = "MemberService")
    private MemberServiceImpl memberService;

    @Resource
    private MemberMapper memberMapper;


    @PostMapping(value = "getMemberList")
    public Map<String,Object> getMemberList(HttpServletRequest request,@RequestParam("page") int page,@RequestParam("limit") int limit,@RequestParam("role") String role){
        Map<String, Object> res= new HashMap<>();
        if(role.equals("所有权限")){
            List<Member> MemberList = memberService.getAllMemberList();
            int totalCount = memberMapper.queryAllMemberNum();
            res.put("code", 0);
            res.put("totalcount",totalCount);
            res.put("data", MemberList);
            res.put("msg", "");
            return res;
        }else{
            List<Member> MemberList = memberService.getMemberListByRole(page,limit,role);
            int totalCount = memberMapper.queryMemberNumByRole(role);
            res.put("code", 0);
            res.put("totalcount",totalCount);
            res.put("data", MemberList);
            res.put("msg", "");
            return res;
        }

    }

    @PostMapping(value = "resetPassWord")
    public String resetPassWord(HttpServletRequest request,@RequestParam("account") String account){
        memberService.resetPassword(account);
        return "{" +
                "\"statusCode\":\"" + 200 + '\"' +
                ", \"message\":\"" + "重置密码成功" + "\""+
                '}';
    }

    @PostMapping(value = "deleteMember")
    public String deleteMember(HttpServletRequest request,@RequestParam("account") String account){
        memberService.deleteMember(account);
        return "{" +
                "\"statusCode\":\"" + 200 + '\"' +
                ", \"message\":\"" + "删除成功" + "\""+
                '}';
    }

    @PostMapping(value = {"addOperator", "removeRole"})
    public String changeMemberRole(HttpServletRequest request,@RequestParam("account") String account){
        String url = request.getServletPath();
        Map<String, String> urlMap = new HashMap<String, String>() {
            {
                put("/admin/removeRole", "队员");
                put("/admin/addOperator", "接线员");
            }

        };
        String memberaccount = memberMapper.memberIsExist(account);
        if(memberaccount!= null){
            if(memberService.changeRole(account,urlMap.get(url))){
                return "{" +
                        "\"statusCode\":\"" + 200 + '\"' +
                        ", \"message\":\"" + "修改权限成功" + "\""+
                        '}';
            }else{
                return "{" +
                        "\"statusCode\":\"" + 400 + '\"' +
                        ", \"message\":\"" + "修改失败，权限不符合修改规定" + "\""+
                        '}';
            }
        }else{
            return "{" +
                    "\"statusCode\":\"" + 202 + '\"' +
                    ", \"message\":\"" + "修改失败，没有找到该成员信息，请先添加成员" + "\""+
                    '}';
        }



    }


    @PostMapping(value = "addMember")
    public String addMember(HttpServletRequest request,@RequestParam("account") String account, @RequestParam("name") String name){
        String memberaccount = memberMapper.memberIsExist(account);
        if(memberaccount !=null){
            return "{" +
                    "\"statusCode\":\"" + 400 + '\"' +
                    ", \"message\":\"" + "成员已存在,请勿重复添加！" + "\""+
                    '}';
        }else{
            memberService.addMember(account,name);
            return "{" +
                    "\"statusCode\":\"" + 200 + '\"' +
                    ", \"message\":\"" + "添加成员成功" + "\""+
                    '}';
        }

    }

}
